﻿using System.Globalization;
using Google.Analytics.Model.Queries;
using Google.Analytics.Web.Converters;
using NUnit.Framework;

namespace Google.Analytics.WebTests
{
    public class SortConverterTests : ConverterTestBase<SortConverter, Order>
    {
        [Test]
        public void ConvertFromSingle()
        {
            var expected = new[] { new Order(Metrics.Visits, SortDirection.Des),  };
            var result = Converter.ConvertFrom(GetTypeContext(), CultureInfo.CurrentCulture, "-Visits");
            VerifyConverFromResult(result, expected);
        }

        [Test]
        public void ConvertFromMultiple()
        {
            var expected = new[] { new Order(Metrics.Visits, SortDirection.Des), new Order(Metrics.TimeOnSite, SortDirection.Asc),  };
            var result = Converter.ConvertFrom(GetTypeContext(), CultureInfo.CurrentCulture, "-Visits,TimeOnSite");
            VerifyConverFromResult(result, expected);
        }

        [Test]
        public void ConvertToSingle()
        {
            var actual = new[] { new Order(Metrics.Visitors, SortDirection.Des) };
            var result = Converter.ConvertTo(GetTypeContext(), CultureInfo.CurrentCulture, actual, typeof(string));
            VerifyCovnertToResult(result, "-Visitors");
        }

        protected override void VerifyConverFromResult(object result, Order[] array)
        {
            Assert.That(result is Order[], "Returned object was not array");
            var resultArray = (Order[])result;
            Assert.AreEqual(array.Length, resultArray.Length, "Invalid size of the array");
            for (int i = 0; i < array.Length; i++)
            {
                Assert.AreEqual(array[i].OrderBy, resultArray[i].OrderBy, "Invalid array item");
                Assert.AreEqual(array[i].SortBy, resultArray[i].SortBy, "Invalid array item");
            }
        }
    }
}
